// pages/myCenter/myAccount.js
const controller=require("../controllers/Controller").controller

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tab1List:[
      {
        name:"奖励记录",
        id:1
      },{
        name:"消费记录",
        id:2
      }
    ],
    tab2List:[{
      name:"全部",
      id:1
    },{
      name:"一卡通",
      id:2
    },{
      name:"店铺爆款",
      id:3
    },{
      name:"欢乐拼",
      id:4
    },{
      name:"限时抵扣",
      id:5
    },{
      name:"超值礼包",
      id:6
    }],
    tab1Index:0,
    tab2Index:0,
    showSecondTab:false,
    detail:"",
    list:"",
    value:{
      pageNumber:1,
      genre:0,
      amountType:0,
      type:"",
      types:'5,10,12,19,20,21,22',
      pageSize:15
    },
    lastPage:false
  },
  //跳转提现记录
  toCashOutHistory(){
    wx.navigateTo({
      url: '/pages/myCenter/cashOutHistory',
    })
  },

  //切换tab
  changeTab(e){
    let index=e.currentTarget.dataset.index
    let value=this.data.value
    if(index==1){
      this.setData({
        showSecondTab:true
      })
      value.genre=1
      value.types="5,10,12,20,21,22,23"
      value.pageNumber=1
      this.setData({
        value:value
      })
      this.getTransactionList()
    }else{
      this.setData({
        showSecondTab:false
      })
      value.genre=0
      value.types="5,10,12,19,20,21,22,23"
      value.pageNumber=1
      this.setData({
        value:value
      })
      this.getTransactionList()
    }
    this.setData({
      tab1Index:index
    })
  },

  //切换二级tab
  changeSecondTab(e){
    let index=e.currentTarget.dataset.index
    let value=this.data.value
    if(index===0){
      value.types="5,10,12,20,22,23",
      value.pageNumber=1
      this.setData({
        value:value
      })
      this.getTransactionList()
    }else if(index==1){
      value.types=10
      value.pageNumber=1
      this.setData({
        value:value
      })
      this.getTransactionList()
    }else if(index==2){
      value.types=12
      value.pageNumber=1
      this.setData({
        value:value
      })
      this.getTransactionList()
    }else if(index==3){
      value.types=20
      value.pageNumber=1
      this.setData({
        value:value
      })
      this.getTransactionList()
    }else if(index==4){
      value.types=22
      value.pageNumber=1
      this.setData({
        value:value
      })
      this.getTransactionList()
    }else{
      value.types=23
      value.pageNumber=1
      this.setData({
        value:value
      })
      this.getTransactionList()
    }
    this.setData({
      tab2Index:index
    })
  },

  //跳转提现
  cashOut(){
    wx.navigateTo({
      url: '/pages/myCenter/cashOut',
    })
  },

  //获取余额信息
  getMoneyMsg(){
    controller.getAccountMsg({amountType:0,types:'5,10,12,19,20,21,22'}).then(res=>{
      this.setData({
        detail:res.data
      })
    })
  },

  //获取账单
  getTransactionList(){
    let value=this.data.value
    controller.getTransactionList(value).then(res=>{
      this.setData({
        list:res.data.list,
        lastPage:res.data.lastPage
      })
    })
  },

  //获取更多
  getMore(){
    if(this.data.lastPage==false){
      let value=this.data.value
      value.pageNumber=value.pageNumber+1
      this.setData({
        value:value
      })
      controller.getTransactionList(value).then(res=>{
        let list=this.data.list
        let list2=res.data.list
        let list3=list.concat(list2)
        this.setData({
          list:list3,
          lastPage:res.data.lastPage
        })
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getTransactionList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getMoneyMsg()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getMore()
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})